package com.example.file.web;

import com.example.common.entity.ResponseCode;
import com.example.common.entity.Rsp;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

@RestController
public class FileUpController {

    @RequestMapping("file-up")
    public Rsp fileUp(MultipartFile file) throws IOException {
        String filename1=file.getOriginalFilename();
        String end=filename1.substring(filename1.lastIndexOf("."));
        //获取文件名
        String filename = UUID.randomUUID().toString()+end;
        //图片的上传路径
        String path="D:\\apache-tomcat-9.0.33\\webapps\\image";
        //创建file对象
        File f = new File(path+File.separator+filename);
        file.transferTo(f);

        String src = "http://localhost:80/image/"+filename;

        return new Rsp(ResponseCode.SUCCESS,src,"上传成功");
    }
}
